﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tasker.Composition;
using Tasker.Infrastructure;
using Tasker.Infrastructure.Entities;
using Tasker.Infrastructure.Repositories.Base;
using Tasker.Infrastructure.Services;
using Tasker.WebUI.Models.Profile;

namespace Tasker.WebUI.Controllers
{
    [Authorize]
    public class ProfileController : TaskerControllerBase
    {
        //
        // GET: /Profile/
        private IAccountService _account = Resolver.Resolve<IAccountService>();
        private IPictureManagerService _pictureManager = Resolver.Resolve<IPictureManagerService>(); 

        public ActionResult Index()
        {
            IUser user = _account.SelectUser(CurrentUserLogin);
            ProfileShowModel model = new ProfileShowModel();

            model.Login = user.Login;
            model.FirstName = user.FirstName;
            model.LastName = user.LastName;
            model.Position = user.Position;
            model.Comment = user.Comment;
            model.Emails = _account.GetUserEmails(user.Id);
            model.Phones = _account.GetUserPhones(user.Id);

            model.PictureName = user.PictureName;
            model.PictureType = user.PictureType;
            
            return View("Show", model);
        }

        public ActionResult Edit()
        {
            IUser user = _account.SelectUser(CurrentUserLogin);
            ProfileEditModel model = new ProfileEditModel();

            model.FirstName = user.FirstName;
            model.LastName = user.LastName;
            model.Position = user.Position;
            model.Comment = user.Comment;
            model.Emails = _account.GetUserEmails(user.Id);
            model.Phones = _account.GetUserPhones(user.Id);
            model.PictureName = user.PictureName;
            model.PictureType = user.PictureType;

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(ProfileEditModel model, string returnUrl, IEnumerable<string> emails, IEnumerable<string> phones )
        {
            var user = _account.SelectUser(CurrentUserLogin);
            if(ModelState.IsValid)
            {
                var context = Resolver.Resolve<IContextManager>();
                context.Begin();

                user.FirstName = model.FirstName;
                user.LastName = model.LastName;
                user.Position = model.Position;
                user.Comment = model.Comment;

                _account.UpdateUser(user);

                _account.DeleteAllEmails(user.Id);
                if (emails != null)
                {
                    foreach (var email in emails)
                    {
                        _account.CreateEmail(user.Id, email);
                    }
                }

                _account.DeleteAllPhones(user.Id);
                if (phones != null)
                {
                    foreach (var phone in phones)
                    {
                        _account.CreatePhone(user.Id, phone);
                    }
                }

                context.Commit();

                if (!string.IsNullOrEmpty(returnUrl))
                    return Redirect(returnUrl);
                else
                    return RedirectToAction("Index");
            }
            model.Emails = _account.GetUserEmails(user.Id);
            model.Phones = _account.GetUserPhones(user.Id);
            return View(model);
        }

        [HttpPost]
        public ActionResult UploadPicture(HttpPostedFileBase fileUpload)
        {
            int w, h;

            var name = _pictureManager.SaveTempPicture(fileUpload.InputStream, out w, out h);
            return Json(new { name, type = fileUpload.ContentType, size = fileUpload.ContentLength, w, h });
        }

        [HttpPost]
        public ActionResult CropPicture(int iw, int ih, int x, int y, int w, int h, string filename)
        {
            if (filename != "")
            {
                var context = Resolver.Resolve<IContextManager>();

                context.Begin();

                var user = _account.SelectUser(CurrentUserId);
                if (user == null)
                    throw new Exception("You are not authorizated to this action");

                _pictureManager.MakePictures(filename, iw, ih, x, y, w, h);
                string oldPicture = user.PictureName;

                user.PictureName = filename;
                user.PictureType = "jpg";

                _account.UpdateUser(user);

                context.Commit();

                if (oldPicture != null)
                    _pictureManager.Delete(oldPicture);
            }
            return RedirectToAction("Edit");
        }
    }
}
